package com.company.emallbackend.controller;

import com.company.emallbackend.common.Result;
import com.company.emallbackend.entity.Cart;
import com.company.emallbackend.entity.dto.CartDTO;
import com.company.emallbackend.entity.dto.UpdateCartDTO;
import com.company.emallbackend.service.ICartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* <p>
*  前端控制器
* </p>
*
* @author hanlijian
* @since 2023-01-12
*/
@RestController
@RequestMapping("/cart")
@Api(tags = "购物车管理")
public class CartController {

    @Resource
    private ICartService cartService;

    /**
     * 添加商品到购物车
     * @param cartDTOList
     * @return
     */
    @PostMapping("addToCart")
    @ApiOperation("添加商品到购物车")
    public Result addToCart(@RequestBody List<CartDTO> cartDTOList) {
        cartService.addToCart(cartDTOList);
        return Result.success();
    }

    /**
     * 修改购物车信息
     * @param cartDTOList
     * @return
     */
    @PutMapping("updateCart")
    @ApiOperation("修改购物车信息")
    public Result updateCart(@RequestBody List<CartDTO> cartDTOList){
        cartService.updateCart(cartDTOList);
        return Result.success();
    }

    /**
     * 查询我的购物车列表
     * @return
     */
    @GetMapping("getCartList")
    @ApiOperation("查询我的购物车列表")
    public Result getCartList(){
        List<Cart> cartList=cartService.getCartList();
        return Result.success(cartList);
    }

    /**
     * 删除购物车信息
     * @param cartId
     * @return
     */
    @DeleteMapping("deleteCart")
    @ApiOperation("删除购物车信息")
    public Result deleteCart(@RequestParam Integer cartId){
        cartService.deleteCart(cartId);
        return Result.success("删除成功");
    }

    /**
     * 修改购物车选中状态
     * @param updateCartDTO
     * @return
     */
    @PutMapping("updateCartStatus")
    @ApiOperation("修改购物车选中状态")
    public Result updateCartStatus(@RequestBody UpdateCartDTO updateCartDTO){
        cartService.updateCartStatus(updateCartDTO);
        return Result.success("修改成功");
    }
}
